package com.yanqu.road.server.gameplayer.module.player;

import com.yanqu.road.entity.voucher.UserVoucherData;
import com.yanqu.road.logic.bussiness.voucher.VoucherBusiness;
import com.yanqu.road.pb.voucher.VoucherProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 代金券模块
 */
public class VoucherModule extends GeneralModule {

    private Map<Integer, List<UserVoucherData>> userVoucherDataMap = new ConcurrentHashMap<>();

    public VoucherModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        userVoucherDataMap = VoucherBusiness.getUserVoucherDataMap(player.getUserId());
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        for (List<UserVoucherData> list : userVoucherDataMap.values()) {
            for (UserVoucherData userVoucherData : list) {
                if (userVoucherData.isInsertOption()) {
                    VoucherBusiness.addUserVoucherData(userVoucherData);
                } else if (userVoucherData.isUpdateOption()) {
                    VoucherBusiness.updateUserVoucherData(userVoucherData);
                }
            }
        }
        return true;
    }

    /**
     * 添加代金券
     *
     * @param userVoucherDataList 代金券数据
     */
    public void addVoucherData(List<UserVoucherData> userVoucherDataList) {
        for (UserVoucherData data : userVoucherDataList) {
            List<UserVoucherData> list = userVoucherDataMap.computeIfAbsent(data.getType(), k -> new CopyOnWriteArrayList<>());
            list.add(data);
        }

        // 添加完代金券同步下数据
        syncVoucherMsg();
    }

    @Override
    public void afterLogin() {
        syncVoucherMsg();
    }

    public void syncVoucherMsg() {
        // 同步下身上的代金券兑换码
        if (userVoucherDataMap.isEmpty()) {
            return;
        }
        VoucherProto.VoucherSyncMsg.Builder syncMsg = VoucherProto.VoucherSyncMsg.newBuilder();
        long nowTime = System.currentTimeMillis();
        for (List<UserVoucherData> list : userVoucherDataMap.values()) {
            for (UserVoucherData userVoucherData : list) {
                // 检验有效期
                if (userVoucherData.getExpireTime() < nowTime) {
                    continue;
                }
                VoucherProto.UserVoucherDataTemp.Builder temp = VoucherProto.UserVoucherDataTemp.newBuilder();
                temp.setVoucherId(userVoucherData.getVoucherId());
                temp.setExpireTime(userVoucherData.getExpireTime());
                temp.setType(userVoucherData.getType());
                temp.setCode(userVoucherData.getCode());
                temp.setPrice(userVoucherData.getPrice());
                syncMsg.addVoucherInfoTemp(temp);
            }
        }
        if (syncMsg.getVoucherInfoTempCount() > 0) {
            player.sendPacket(Protocol.U_VOUCHER_DATA_SYNC, syncMsg);
        }
    }
}
